<?php

require_once(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR ."core".DIRECTORY_SEPARATOR."functions.php");
require_once("Database.php");


class Activity extends dbConn
{
	private $activityId;
	private $title,$description;
	private $startDate,$endDate,$startTime,$endTime;
	private $event;
	private $dateCreated;

	public function __construct($activityId)
	{
		$this->activityId = $activityId;
		$this->fetchData();
	}

	private function fetchData()
	{
		$res = $this->query("SELECT * FROM activity WHERE activityId=$this->activityId");
		
		if($this->num_rows($res) == 1)
		{
			$data = $this->fetch_array($res);
			$this->title = $data[A_T];
			$this->description = $data[A_D];
			$this->startDate = $data[A_SD];
			$this->endDate = $data[A_ED];
			$this->startTime = $data[A_ST];
			$this->endTime = $data[A_ET];
			$this->event = $data[A_E];
			$dateCreated = $data[A_DC];
		}
		else
		{
			die("NO DATA FROM id $this->activityId");
		}
	}
    
    public static function exists($activityId)
	{
		$dbConn = new dbConn();
		if($dbConn->num_rows($dbConn->query("SELECT activityId FROM activity WHERE activityId = $activityId")) > 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public static function getActivitiesByDate($startDate)
	{
		$conn = new dbConn();
		$cnd = "startDate>=$startDate";
		$query = "SELECT activityId,title FROM activity WHERE ".$cnd;
		$res = $conn->query($query);
		if($conn->num_rows($res) >= 1)
		{
			$array = array();
			while($row = $conn->fetch_array($res))
			{
				$array[] = $row;
			}
			return ($array);
		}
		else
		{
			return null;
		}
	}

	public static function getActivitiesNum($eventId)
	{
		$db = new dbConn();
		$cnd = "event=$eventId";
		$res = $db->query("SELECT * FROM activity WHERE ".$cnd);
		return ($db->num_rows($res));
	}

	public static function getActivityByEventId($id,$all = false)
	{
		$db = new dbConn();
		$cnd = "event=$id";
		if($all)
		{
			$query = "SELECT * FROM activity WHERE ".$cnd;
		}
		else
		{
			$query = "SELECT activityId,title FROM activity WHERE ".$cnd;
		}
		$res = $db->query($query);
		if($db->num_rows($res) >= 1)
		{
			$array = array();
				while($row = $db->fetch_array($res,$all))
				{
					$array[] = $row;
				}
			
			return ($array);
		}
		else
		{
			return null;
		}
	}
	
	//GETTERS AND SETTERS
	public function __get($var)
	{
		if(isset($this->$var))
		{
			return $this->$var;
		}
		else
		{
			return "NULL";
		}
	}

	public function setTitle($title)
	{
		$this->title = $title;
	}

	public function setDescription($description)
	{
		$this->description = $description;
	}

	public function setStartDate($startDate)
	{
		$this->startDate = $startDate;
	}

	public function setEndDate($endDate)
	{
		$this->endDate = $endDate;
	}

	public function setStartTime($startTime)
	{
		$this->startTime = $startTime;
	}

	public function setEndTime($endTime)
	{
		$this->endTime = $endTime;
	}

	public function setEvent($event)
	{
		$this->event = $event;
	}

	public static function deleteActivity($id,$isActivity=true)
	{
		$dbConn = new dbConn();
		//id can be a event id or activity id
		Role::deleteRole($id,false);
		if($isActivity)
		{
			if($dbConn->num_rows($dbConn->query("SELECT * FROM activity WHERE activityId=$id"))>0)
			{
				$dbConn->delete("DELETE FROM activity WHERE activityId=$id");	
			}
		}
		else
		{
			if($dbConn->num_rows($dbConn->query("SELECT * FROM activity WHERE event=$id"))>0)
			{
				$dbConn->delete("DELETE FROM activity WHERE event=$id");
			}
		}
	}

	public static function updateActivity($activityId,$title,$startTime,$endTime,$startDate,$endDate,$description,$eventId)
	{
		$dbConn = new dbConn();
		$res = $dbConn->query("UPDATE activity SET ".A_T."='$title',".A_D."='$description',".A_ST."='$startTime',".A_ET."='$endTime',".A_SD."='$startDate',".A_ED."='$endDate',".A_E."=$eventId WHERE activityId=$activityId");
	}
    
    public function getRoles()
	{
		$res = $this->query("SELECT roleId,title from role WHERE activity=$this->activityId");
		if($this->num_rows($res) > 0)
		{
			$array = array();
			while($data = $this->fetch_array($res))
			{
				$array[] = $data;
			}
			return $array;
		}else{
			return null;
		}
	}
}


?>